﻿using AutoMapper;
using MediatR;
using Production_Get.API.DTO;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Get.API.Application.Commands.ResourcteShow
{
    /// <summary>
    /// 绑定资源下拉框信息
    /// </summary>
    public class ResourceShowHandler : IRequestHandler<ResourceShowCommand, Result>
    {
        private readonly IBaseRepositoryGet<Resource> ResourceRepository;
        private readonly ILogger<ResourceShowHandler> logger;

        public ResourceShowHandler(IBaseRepositoryGet<Resource> resourceRepository, ILogger<ResourceShowHandler> logger)
        {
            ResourceRepository = resourceRepository;
            this.logger = logger;
        }

        public async Task<Result> Handle(ResourceShowCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };
            try
            {
                string sql = "SELECT * FROM Resource";
                Resource come = new Resource();
                var list = ResourceRepository.GetAll(sql, come).ToList();
                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = list;
                    result.Msg = "绑定成功";
                    logger.LogInformation($"绑定资源下拉框信息成功");
                }
                else
                {
                    result.Msg = "绑定失败";
                    logger.LogError($"绑定资源下拉框信息失败");
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"绑定资源下拉框信息失败," + ex.Message);
                throw;
            }
            return result;
        }
    }
}
